package com.huyun.web.FutureTest;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 毕业证书
 */
public class WaterMarkUtils {

    private static final Font font = new Font("微软雅黑", Font.BOLD, 100);
    private static final String srcImgPath="E:\\zs.png"; //源图片地址
    private static final Color color=new Color(0,0,0,0256);
    //设置坐标
    private static final int[][] position = {{870,860},{1600, 860},{1910, 860},{2400, 860},{2700, 860}
            ,{510,1160},{900,1160},{1150,1160},{1700,1160},{2200,1160},{2500,1160}
            ,{1850,2120},{2380,2120},{2700,2120},{850,2110}};
    /**
     * @param srcImgPath 源图片路径
     * @param tarImgPath 保存的图片路径
     * @param waterMarkContent 水印内容
     * @param position 水印位置
     * @param markContentColor 水印颜色
     * @param font 水印字体
     */

    public static void addWaterMark(String srcImgPath, String tarImgPath, String[] waterMarkContent,int[][] position,Color markContentColor,Font font) {
        try {
            File srcImgFile = new File(srcImgPath);
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //设置颜色
            g.setFont(font);              //设置字体

            //设置内容坐标
            for (int i=0;i<waterMarkContent.length;i++){
                if (i==waterMarkContent.length-1){
                    g.setColor(new Color(255, 33, 42, 255) );
                    g.setFont(new Font("仿宋", Font.PLAIN, 70));
                }
                g.drawString(waterMarkContent[i], position[i][0], position[i][1]);  //合成
            }
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            String prefix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1);
            ImageIO.write(bufImg, prefix, outImgStream);
            System.out.println("生成证书完成");
            outImgStream.flush();
            outImgStream.close();

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static Integer getImg(){
        Long stat = System.currentTimeMillis();
        ExecutorService service = Executors.newFixedThreadPool(15);
        for(int i=0;i<15;i++){
            WaterMarkTask myTask = new WaterMarkTask(i);
            service.execute(myTask);
        }
        Long end = System.currentTimeMillis();
        System.out.println(end-stat);
        service.shutdown();
        return 1;
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Long stat = System.currentTimeMillis();
        System.out.println("======"+stat);
        //用多线程大概13s 单线程33s
        ExecutorService service = Executors.newFixedThreadPool(15);
        for(int i=0;i<15;i++){
            WaterMarkTask myTask = new WaterMarkTask(i);
            service.execute(myTask);
        }
        Long end = System.currentTimeMillis();
        System.out.println(end-stat);
        service.shutdown();
        //异步执行
//        CompletableFuture<Integer> cp3 = CompletableFuture.supplyAsync((()-> getImg()));
//        System.out.println(cp3.get());
        System.out.println("-----------------------");

    }
}
